﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Parser
{
    /// <summary>
    /// 表示TOKEN基类型
    /// </summary>
    /// <typeparam name="TokenType">实际的记号类型</typeparam>
    public abstract class TokenBase<TokenType>
    {
        bool    _stateOpen;
        bool    _stateClose;
        int     _offset;
        int     _length;
        string  _text;
        int     _stateId;
        

        public TokenBase(int stateId,int offset,int length,string text)
        {
            _stateId = stateId;
           
            if (length < 0) {
                throw new ArgumentOutOfRangeException("长度必须大于等于0");
            }
            _length = length;
            _text = text ?? throw new NullReferenceException();

            if (_offset < 0 || _offset >= _text.Length) {
                throw new ArgumentOutOfRangeException("字符截取不应该在流的末端");
            }
            _offset = offset;
        }

        #region Property

        /// <summary>
        /// 返回当前记号类型
        /// </summary>
        public TokenType Type {
            get {
                return (TokenType)Enum.Parse(typeof(TokenType),_stateId.ToString());
            }
        }

        /// <summary>
        /// 表示生成当前记号的状态是否开启
        /// </summary>
        public bool StateOpen { get => _stateOpen; set => _stateOpen = value; }

        /// <summary>
        /// 表示生成当前记号的状态是否关闭
        /// </summary>
        public bool StateClose { get => _stateClose; set => _stateClose = value; }

        /// <summary>
        /// 获取当前记号的文本
        /// </summary>
        public string GetString {
            get {
                return _text.Substring(_offset, _length);
            }
        }

        public override string ToString()
        {
            return $"<{Type.ToString()},{GetString}>";
        }

        #endregion
    }
}
